﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;

namespace eLearning.View
{
    public partial class WikiLogPage : PhoneApplicationPage
    {
        private Wiki wiki;

        public WikiLogPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RssItemPage_Loaded);
        }

        private void RssItemPage_Loaded(object sender, RoutedEventArgs e)
        {
            wiki = (Wiki)DataContext;
            if (wiki != null)
            {
                GetWikiLog(wiki);
            }
        }

        private void NavigateToDetail(Wiki selectedWiki)
        {
            textBoxJudul.Text = selectedWiki.Judul;
            textBoxUpdated.Text = selectedWiki.Created.ToString();
            textBoxUpdater.Text = selectedWiki.Creator.Nama;
            textBoxIsi.Text = selectedWiki.Isi;
            pivotWiki.SelectedIndex = 1;
        }

        private void GetWikiLog(Wiki currentWiki)
        {
            string url = null, json = null;

            url = Config.Instance.WIKI_LOG_SERVICE_URL() + Config.FIND_ACTION_KEY;
            json = JsonConvert.SerializeObject(currentWiki);

            string urlformat = string.Format("{0}&{1}", url, Guid.NewGuid().ToString());
            Uri uri = new Uri(urlformat, UriKind.Absolute);

            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
            webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(listWikiLog_UploadStringCompleted);

            webClient.UploadStringAsync(uri, "POST", json);
        }

        private void listWikiLog_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled || e.Error != null)
                {
                    MessageBox.Show("Message : " + e.Result);
                    return;
                }

                WikiList list = JsonConvert.DeserializeObject<WikiList>(e.Result);
                listWikiLog.ItemsSource = list;
                pivotWiki.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void listWikiLog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                wiki = (Wiki)e.AddedItems[0];

                ((ListBox)sender).SelectedIndex = -1;
                NavigateToDetail(wiki);
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
            {
                GetWikiLog(wiki);
            }
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            if (pivotWiki.SelectedIndex > 0 && listWikiLog.Items.Count > 0)
                pivotWiki.SelectedIndex = 0;
            else
                NavigationService.GoBack();
        }

    }
}